<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="位置" prop="locationCode">
        <TreeSelect
          v-model="formItem.locationCode"
          :multiple="false"
          :options="availableLocations"
          value-consists-of="LEAF_PRIORITY"
          placeholder="请选择位置"
        />
      </el-form-item>
      <el-form-item label="库房名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入库房名称" />
      </el-form-item>
      <!--
      <el-form-item label="状态" prop="status">
        <el-select v-model="formItem.status" clearable placeholder="请选择状态">
          <el-option v-for="item in storehouseStatus" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
      -->
    </el-form>
  </div>
</template>

<script>
import TreeSelect from '@neu/biz-core/components/TreeSelect'
import {
  create,
  update,
  detail,
  status
} from '@neu/dcam-ui/api/storehouse'
import { tree } from '@neu/dcam-ui/api/location'

// 定义一个表单原始项
const formItem = {
  name: '',
  locationCode: null,
  status: 'enable'
}
export default {
  name: 'StorehouseEditForm',
  components: {
    TreeSelect
  },
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 位置树
      availableLocations: [],
      storehouseStatus: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '库房名称不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        locationCode: [
          { required: true, message: '位置不能为空', trigger: 'blur' }
        ],
        status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
      },
      modal: true
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    tree().then(response => {
      this.availableLocations = response.data
    })
    // 获取盘点结果状态
    status().then(res => { this.storehouseStatus = res.data })
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
